package com.lin.subject.infra.basic.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.subject.commom.enums.IsDeletedFlagEnum;
import com.lin.subject.infra.basic.entity.SubjectMapping;
import com.lin.subject.infra.basic.mapper.SubjectMappingDao;
import com.lin.subject.infra.basic.service.SubjectMappingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
* @author jeremylin
* @description 针对表【subject_mapping(题目分类关系表)】的数据库操作Service实现
* @createDate 2024-10-21 09:14:06
*/
@Service
public class SubjectMappingServiceImpl extends ServiceImpl<SubjectMappingDao, SubjectMapping>
    implements SubjectMappingService {

    @Resource
    private SubjectMappingDao subjectMappingDao;
    /**
     * 根据分类Id查询Labelud
     * @param categoryId
     * @return
     */
    @Override
    public List<SubjectMapping> getByCategoryId(Long categoryId) {
        LambdaQueryWrapper<SubjectMapping> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(SubjectMapping::getLabelId)
                .eq(SubjectMapping::getCategoryId, categoryId)
                .eq(SubjectMapping::getCategoryId, categoryId)
                .eq(SubjectMapping::getIsDeleted, IsDeletedFlagEnum.UN_DELETED.getCode())
                .groupBy(SubjectMapping::getLabelId);

        return subjectMappingDao.selectList(queryWrapper);
    }

    @Override
    public void batchInsert(LinkedList<SubjectMapping> subjectMappings) {
        this.saveBatch(subjectMappings);
    }

    /**
     * 根据题目id，查询所有映射关系
     *
     * @param id
     * @return
     */
    @Override
    public List<SubjectMapping> getBySubjectId(Long id) {
        return lambdaQuery().eq(SubjectMapping::getSubjectId,id).list();
    }

    @Override
    public List<SubjectMapping> queryLabelId(SubjectMapping subjectMapping) {
        return lambdaQuery()
                .eq(Objects.nonNull(subjectMapping.getLabelId()),SubjectMapping::getLabelId,subjectMapping.getLabelId())
                .list();
    }

    /**
     * 根据题目类型，查询对应的count
     *
     * @return
     */
    @Override
    public List<SubjectMapping> querySubjectCount() {
        QueryWrapper<SubjectMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("category_id","count(*) as count");
        queryWrapper.groupBy("category_id");
        return subjectMappingDao.selectList(queryWrapper);
    }

    /**
     * 根据条件查找
     *
     * @param subjectMapping
     * @return
     */
    @Override
    public List<SubjectMapping> queryCondition(SubjectMapping subjectMapping) {
        return lambdaQuery()
                .eq(Objects.nonNull(subjectMapping.getCategoryId()),SubjectMapping::getCategoryId,subjectMapping.getCategoryId())
                .eq(Objects.nonNull(subjectMapping.getSubjectId()),SubjectMapping::getSubjectId,subjectMapping.getSubjectId())
                .list();
    }

    /**
     * 删除映射关系
     *
     * @param id
     */
    @Override
    public void removeMapping(Long id) {
        lambdaUpdate()
                .eq(SubjectMapping::getSubjectId,id)
                .set(SubjectMapping::getIsDeleted,IsDeletedFlagEnum.DELETED.getCode())
                .update();
    }
}




